package com.example.projetoandroid.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class NoticiaPersistencia {
	  private static NoticiaPersistencia instance;
	  private SQLiteDatabase db;  
	  private SQLiteHelper dbHelper;  
	  private static final String SCRIPT_DB_DELETE =   
	    "DROP TABLE IF EXISTS noticia";  
	  private static final String SCRIPT_DB_CREATE =  
	    "create table noticia (_id integer primary "+  
	    "key autoincrement, titulo text not null, "+  
	    "descricao text not null, link text not null," +
	    "lido integer not null, favorito integer not null," +
	    "pagina integer not null, feed integer not null);";  
	  
	  public NoticiaPersistencia (Context ctx){  
	    dbHelper = new SQLiteHelper(ctx, "curso", 1,   
	      SCRIPT_DB_CREATE, SCRIPT_DB_DELETE);  
	  }
	  
	  public static NoticiaPersistencia getInstance(Context context) {
			if (instance == null) {
				synchronized (NoticiaPersistencia.class) {
	 
					if (instance == null) {
	 
						instance = new NoticiaPersistencia(context);
	 
						/*db = context.openOrCreateDatabase(
								DatabaseCreate.TUTORIAL_DB, Context.MODE_PRIVATE,
								null);*/ // Como corrige esse erro?
					}
				}
			}
			return instance;
		}
	  
	  public long cadastrarNoticia(Noticia n){
	    ContentValues cv = new ContentValues();  
	    cv.put("titulo", n.getTitulo());
	    cv.put("descricao", n.getDescricao());  
	    cv.put("link", n.getLink());
	    cv.put("lido", n.isLido());
	    cv.put("favorito", n.isFavorito());
	    cv.put("pagina", n.getPagina().get_id());
	    cv.put("feed", n.getFeed().get_id());
	    db = dbHelper.getWritableDatabase();  
	    long id = db.insert("noticia", null, cv);  
	    db.close();  
	    return id;  
	  }  
	  
	  public long atualizarNoticia(Noticia n){
	    ContentValues cv = new ContentValues();  
	    cv.put("titulo", n.getTitulo());
	    cv.put("descricao", n.getDescricao());  
	    cv.put("link", n.getLink());
	    cv.put("lido", n.isLido());
	    cv.put("favorito", n.isFavorito());
	    cv.put("pagina", n.getPagina().get_id());
	    cv.put("feed", n.getFeed().get_id());
	    db = dbHelper.getWritableDatabase();  
	    long rows = db.update("noticia", cv, "_id = ?",   
	       new String[]{ String.valueOf(n.get_id())});  
	    db.close();  
	    return rows; // qtde. de linhas afetadas   
	  }   
	  
	  public int deletarNoticia(int id){  
	    db = dbHelper.getWritableDatabase();  
	    int rows = db.delete("noticia", "_id = ?",   
	      new String[]{ String.valueOf(id) });  
	    return rows; // qtde. de linhas afetadas  
	  }  
	  
	  /*public List<Carro> buscarCarroPorNome(String nome){  
	    List<Carro> lista = new ArrayList<Carro>();  
	  
	    String[] columns = new String[]{  
	       "_id", "nome", "placa", "ano"};  
	    String[] args = new String[]{nome+"%"};  
	  
	    db = dbHelper.getWritableDatabase();  
	    Cursor c = db.query("carros", columns,   
	       "nome like ?", args, null, null, "nome");  
	  
	    c.moveToFirst();  
	    while(!c.isAfterLast()){  
	      Carro carro = fillCarro(c);  
	      lista.add(carro);  
	      c.moveToNext();  
	    }  
	    c.close();  
	    db.close();  
	    return lista;  
	  }  */ //Ajeitar
	  
	  public Noticia fillNoticia(Cursor c) {
	    Noticia noticia = new Noticia();  
	    noticia.set_id((int)c.getLong(0));
	    noticia.setTitulo(c.getString(1));
	    noticia.setDescricao(c.getString(2));  
	    noticia.setLink(c.getString(3));  
	    //noticia.setLido(c.getString(4));
	    //noticia.setFavorito(c.getString(5));
	    //noticia.setPagina(c.getString(6));
	    //noticia.setFeed(c.getString(7));
	    return noticia;  
	  }  
	}
